// pages/sale/fillin/fillin.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 0, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '填写退货物流', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    loadModal: true,
    refund_logistics_no: '', //物流单号
    refund_link_phone: '', //联系电话
    refund_notice: '', //描述
    refundText: '',
    CSrefundText: '',
    refundid: '',
    CSrefundid: '',
    images: [], //图片
    height: app.globalData.height,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      id: options.id,
      goods: wx.getStorageSync('goods')
    })
    $api.getCompanyAndPhone({
      login_token: wx.getStorageSync('login_token'),
    }).then((res) => {
      var list = [];
      for (let i in res.data.data.company_list) {
        list = list.concat({
          text: res.data.data.company_list[i],
          icon: false,
          id: i
        })
      }
      console.log(list)
      _this.setData({
        data: list,
        refund_link_phone: res.data.data.user_mobile,
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  scroll: function (e) {
    // console.log(e)
    this.setData({
      scrollTop: e.detail.scrollTop
    }); 
    if (e.detail.scrollTop >= 40) {
      this.setData({
        headerBg: true
      })
    } else {
      this.setData({
        headerBg: false
      })
    }
  },
  //返回上一页
  out() {
    wx.navigateBack({
      delta: 1,
    })
  },
  //获取输入内容
  onInput(e) {
    var _this = this;
    if (e.currentTarget.dataset.id == 1) {
      _this.setData({
        refund_logistics_no: e.detail.value
      })
    }
    if (e.currentTarget.dataset.id == 2) {
      _this.setData({
        refund_link_phone: e.detail.value
      })
    }
    if (e.currentTarget.dataset.id == 3) {
      _this.setData({
        refund_notice: e.detail.value
      })
    }
  },
  //物流公司弹出
  showModal(e) {
    var _this = this;
    for (var i = 0; i < _this.data.data.length; i++) {
      if (_this.data.refundid == _this.data.data[i].id) {
        _this.setData({
          ['data[' + i + '].icon']: true
        })
      } else {
        _this.setData({
          ['data[' + i + '].icon']: false
        })
      }
    }
    _this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //物流公司关闭
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //物流公司选择
  onRefund(e) {
    var _this = this;
    for (var i = 0; i < _this.data.data.length; i++) {
      if (e.currentTarget.dataset.id == _this.data.data[i].id) {
        _this.setData({
          ['data[' + i + '].icon']: true,
          CSrefundid: e.currentTarget.dataset.id,
          CSrefundText: e.currentTarget.dataset.text
        })
      } else {
        _this.setData({
          ['data[' + i + '].icon']: false
        })
      }
    }


  },
  //确认物流公司
  onRefundConfirm() {
    var _this = this;
    if (_this.data.CSrefundText == '') {
      wx.showToast({
        title: '请选择物流公司',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        refundText: _this.data.CSrefundText,
        refundid: _this.data.CSrefundid,
        modalName: null
      })
    }
  },
  //上传图片
  onUploadImages() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.uploadImages().then((res) => {
      _this.setData({
        images: _this.data.images.concat(res.url),
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //删除图片
  onCloseImages(e) {
    var _this = this;
    var images = _this.data.images;
    images.splice(e.currentTarget.dataset.idx, 1);
    _this.setData({
      images: images
    })
  },
  //提交按钮
  onSubmit() {
    var _this = this;
    var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
    if (_this.data.refund_logistics_no == '') {
      wx.showToast({
        title: '请填写物流单号',
        icon: 'none',
        duration: 2000
      })
    } else if (_this.data.refundText == '') {
      wx.showToast({
        title: '请选择物流公司',
        icon: 'none',
        duration: 2000
      })
    } else if(_this.data.refund_link_phone==''){
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none',
        duration: 2000
      })
    }else if (!reg_tel.test(_this.data.refund_link_phone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        loadModal: true,
      })
      if(_this.data.images.length>0){
        var refund_images = JSON.stringify(_this.data.images);;
      }else{
        var refund_images='';
      }
      

      $api.submitAsaleLogistics({
        login_token: wx.getStorageSync('login_token'),
        asale_id: _this.data.id,
        refund_logistics_no: _this.data.refund_logistics_no,
        refund_company: _this.data.refundText,
        refund_notice: _this.data.refund_notice,
        refund_link_phone: _this.data.refund_link_phone,
        refund_images: refund_images
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1500)
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }


  },
  //查看图片
  onSeeImage(e){
    wx.previewImage({
      current: e.currentTarget.dataset.url, 
      urls: [e.currentTarget.dataset.url] 
    })
  },

})